package com.qwt.part_time_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwt.part_time_api.error.BusinessException;
import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.mapper.TaskMapper;
import com.qwt.part_time_api.pojo.Task;
import com.qwt.part_time_api.service.TaskService;
import com.qwt.part_time_api.service.ThreadService;
import com.qwt.part_time_api.utils.Copy;
import com.qwt.part_time_api.vo.Result;
import com.qwt.part_time_api.vo.TaskDetailVo;
import com.qwt.part_time_api.vo.TaskListVo;
import com.qwt.part_time_api.vo.params.PageParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 君子慎独
 * @create 2021/12/24 0024 13:33
 */

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private ThreadService threadService;

    @Override
    public Result findTaskListByPage(PageParams params) {
        log.info("首页推荐任务列表");
        //使用线程池更新 这样可以避免出现性能过低的情况
        threadService.updateTaskStatus(taskMapper);
        Page<TaskListVo> page = new Page<>(params.getPage(), params.getPageSize());

        IPage<TaskListVo> taskListByPage = taskMapper.findTaskListByPage(page,
                params.getOrder(),
                params.getRegion());
        return Result.success(taskListByPage);
    }

    @Override
    public Result findTaskListByFilter(PageParams params) throws BusinessException {
        log.info("根据筛选条件获取任务列表");
        if (params.getRegion() == null) {
            log.info("无该城市");
            throw new BusinessException(ErrorCode.ERR_NO_SUCH_CITY);
        }
        //使用线程池更新 这样可以避免出现性能过低的情况
        threadService.updateTaskStatus(taskMapper);
        Page<TaskListVo> page = new Page<>(params.getPage(), params.getPageSize());
        IPage<TaskListVo> taskListByFilter = taskMapper.findTaskListByFilter(page,
                params.getOrder(),
                params.getTid(),
                params.getSid(),
                params.getAreaCode(),
                params.getRegion(),
                params.getTitle());
        return Result.success(taskListByFilter);
    }

    @Override
    public Result findTaskNameBySuggestion(String name) {
        log.info("搜索栏的推荐");
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Task::getId, Task::getTitle);
        if (!StringUtils.equals(name, "")) {
            wrapper.like(true,
                    Task::getTitle, name);
        }
        //分组 防止重复
        wrapper.groupBy(Task::getTitle);
        //根据浏览量排序
        wrapper.orderByDesc(Task::getTraffic);
        //返回个数
        wrapper.last("limit 6");
        List<Task> tasks = taskMapper.selectList(wrapper);

        return Result.success(Copy.copySuggestion(tasks));
    }

    @Override
    public Result findTaskDetailById(Long id) {
        log.info("任务详情");
        //使用线程池更新 这样可以避免出现性能过低的情况
        threadService.updateTaskCutoff(taskMapper, id);
        TaskDetailVo taskDetailById = taskMapper.findTaskDetailById(id);
        // 增加访问量
        Long traffic = taskDetailById.getTraffic();
        taskDetailById.setTraffic(traffic + 1);
        //使用线程池更新 这样可以避免出现性能过低的情况
        threadService.updateTaskTraffic(taskMapper, id, traffic);
        return Result.success(taskDetailById);
    }
}

